package com.hibernate.hrm.entity;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import jakarta.persistence.*;
import lombok.Data;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;

@Entity
@Table(name = "users")
@Data
@ToString(exclude = {"departmentID", "jobPositionID", "roleId"})
@JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class,
        property = "userId"
)
public class User implements UserDetails {
    // 性别枚举定义
    public enum Gender {
        男, 女
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "UserID")
    private Integer userId;

    @Column(name = "username", nullable = false, unique = true)
    private String username;

    @Column(name = "password", nullable = false)
    private String password;

    @ManyToOne
    @JoinColumn(name = "roleID")
    private Role roleId;

    @Column(name = "realName")
    private String realName;

    @Enumerated(EnumType.STRING)
    @Column(name = "gender", columnDefinition = "ENUM('男','女')")
    private Gender gender;

    @Column(name = "birthday")
    private LocalDate birthday;

    @Column(name = "age")
    private Integer age;

    @Column(name = "IDnumber")
    private String idNumber;

    @Column(name = "StartingDate")
    private LocalDate startingDate;

    @Column(name = "education")
    private String education;

    @Column(name = "major")
    private String major;

    @Column(name = "email")
    private String email;

    @Column(name = "phone")
    private String phone;

    @ManyToOne
    @JoinColumn(name = "JobPositionID")
    @JsonIgnoreProperties({"departmentID", "positionRoleID"})
    private Position jobPositionID;

    @ManyToOne
    @JoinColumn(name = "DepartmentID")
    @JsonIgnoreProperties({"subDepartments", "departmentParentID"})
    private Department departmentID;

    @Column(name = "status", nullable = false)
    private Boolean status = true;

    @Column(name = "lastLogin")
    private LocalDateTime lastLogin;

    @Column(name = "createTime", nullable = false)
    private LocalDateTime createTime;

    @Column(name = "updateTime")
    private LocalDateTime updateTime;

    // Spring Security 相关方法
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.singletonList(() -> "ROLE_" + roleId.getRoleCode().trim());
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return status;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return status;
    }
}